package gameDump;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Robin
 */
public class Board extends javax.swing.JPanel implements Runnable {

    /**
     * Creates new form Board
     */
    Thread animator;
    private Pacman pacman;
    private Maze maze;

    public Board() {
        //initComponents();

        pacman = new Pacman();
        maze = new Maze();
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent evt) {
                pacman.keyPressed(evt);
            }

            public void keyReleased(KeyEvent evt) {
                pacman.keyReleased(evt);
            }
        });
        setBackground(Color.BLACK);
        setFocusable(true);
        setDoubleBuffered(true);




    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    public void addNotify() {
        super.addNotify();
        animator = new Thread(this);
        animator.start();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;

        maze.DrawMaze(g2d);

        g2d.drawImage(pacman.getImage(), pacman.getX(), pacman.getY(), this);




    }

    public void cycle() {
        pacman.move();
        repaint();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public void run() {


        while (true) {
            cycle();
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }
}
